package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.pojo.BaseSaleAttr;
import com.atguigu.gmall.product.pojo.SpuImage;
import com.atguigu.gmall.product.pojo.SpuInfo;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.atguigu.gmall.product.vo.SpuSaleAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@NoArgsConstructor
@Data
@RequestMapping("/admin/product")
@RestController
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    BaseSaleAttrService baseSaleAttrService;
    @Autowired
    SpuImageService spuImageService;


    @Autowired
    SpuSaleAttrService spuSaleAttrService;


    //http://api.gmall.com/admin/product/1/10?category3Id=61
    /**
     * 分页查询SPU的基本信息
     * @param pn
     * @param ps
     * @param c3id
     * @return
     */
    @GetMapping("/{pn}/{ps}")
    public Result getPageBySpuId(@PathVariable Long pn, @PathVariable Long ps, @RequestParam("category3Id") Long c3id){
        Page<SpuInfo> page = spuInfoService.getPageBySpuId(pn, ps, c3id);
        return Result.ok(page);
    }

    //http://api.gmall.com/admin/product/baseSaleAttrList

    /**
     * 获取SPU属性列表
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result getSaleAttrList(){
        List<BaseSaleAttr> list = baseSaleAttrService.list();
        return Result.ok(list);
    }

    //http://api.gmall.com/admin/product/saveSpuInfo

    /**
     * 新建SPU
     * @param spuInfoVo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfoVo spuInfoVo){
        spuInfoService.saveSpuInfo(spuInfoVo);
        return Result.ok();
    }
    //http://api.gmall.com/admin/product/spuSaleAttrList/45
    /**
     * 查询SpuId 所对应的Spu商品属性
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttrVo> list =  spuSaleAttrService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }

    // http://api.gmall.com/admin/product/spuImageList/45
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        List<SpuImage> list = spuImageService.list(new LambdaQueryWrapper<SpuImage>().eq(SpuImage::getSpuId, spuId));
        return Result.ok(list);
    }




}
